package com.quectel.cms.controller.scheduling;


import com.quectel.business.common.util.sys.SysCodeUtils;
import com.quectel.cms.controller.BaseController;
import com.quectel.constant.core.scheduling.SchedulingAttendanceDailyConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.constant.sys.SysCodeKeyConstants;
import com.quectel.core.module.scheduling.dto.mongo.MongoOriginalCheckRecordDto;
import com.quectel.core.module.scheduling.service.mongo.MongoOriginalCheckRecordService;
import com.quectel.util.common.*;
import com.quectel.util.kit.PageFetchKit;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.compress.utils.Lists;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.quectel.constant.global.StatisticsConstants.TIME_RANGE_BY_MONTH;

/**
 *
 * @author ryan
 * @email ryan.ran@quectel.com
 * @date 2023/07/13 18:58
 */
@RestController
@RequestMapping("schedulingOriginalCheckRecord")
@Tag(name = "SchedulingOriginalCheckRecordController", description = "原始打卡记录相关api")
public class SchedulingOriginalCheckRecordController extends BaseController {

    @DubboReference
    private MongoOriginalCheckRecordService mongoOriginalCheckRecordService;

    /**
     * 导出原始打卡记录
     */
    @GetMapping("exportExcel")
    @RequirePermissions(values = "originalCheckRecord:exportExcel")
    public void exportParkExcel(@RequestParam Map<String, Object> params, HttpServletResponse httpServletResponse) {
        Request request = Request.configParamsNotPage(params);

        super.fillAuthorityData(params);

        if (!params.containsKey(SystemConstants.TIME_SLOT_KEY_PAIR.getLeft()) || !params.containsKey(SystemConstants.TIME_SLOT_KEY_PAIR.getRight())) {
            POIUtils.exportExcel(httpServletResponse, "原始打卡记录", new String[]{"打卡时间范围必传"}, Lists.newArrayList(), "原始打卡记录");
            return ;
        }
        Response<Object> checkResponse = checkTimeRange(TIME_RANGE_BY_MONTH, params.get(SystemConstants.TIME_SLOT_KEY_PAIR.getLeft()) + " 00:00:00"
                , params.get(SystemConstants.TIME_SLOT_KEY_PAIR.getRight()) + " 00:00:00", 1);
        if (!checkResponse.isSuccess()) {
            POIUtils.exportExcel(httpServletResponse, "原始打卡记录", new String[]{checkResponse.getMessage()}, Lists.newArrayList(), "原始打卡记录");
            return ;
        }

        List<MongoOriginalCheckRecordDto> result = PageFetchKit.fetchAll(r -> mongoOriginalCheckRecordService.queryList(r), request.getParams());
        String[] headers = new String[]{"所属小区", "工号", "姓名", "部门", "考勤组", "班制类型", "是否补卡", "是否外勤", "打卡地址", "打卡备注", "打卡图片", "打卡时间"};
        List<String[]> dataList = new ArrayList<>();

        for (MongoOriginalCheckRecordDto data : result) {
            String[] item = new String[headers.length];
            item[0] = Objects.nonNull(data.getVillage()) ? data.getVillage().getName() : "";
            item[1] = Objects.nonNull(data.getDeptPerson()) ? data.getDeptPerson().getJobNo() : "";
            item[2] = Objects.nonNull(data.getDeptPerson()) ? data.getDeptPerson().getName() : "";
            item[3] = Objects.nonNull(data.getDept()) ? data.getDept().getName() : "";
            item[4] = Objects.nonNull(data.getSchedulingAttendanceGroup()) ? data.getSchedulingAttendanceGroup().getName() : "";
            item[5] = Objects.nonNull(data.getSchedulingAttendanceGroup()) ? SysCodeUtils.easyToGetName(SysCodeKeyConstants.SCHEDULING_SHIFT_SYSTEM_TYPE
                    , data.getSchedulingAttendanceGroup().getShiftSystemType() + "") : "";
            item[6] = SchedulingAttendanceDailyConstants.IsRepairEnum.IS_REPAIR.getType().equals(data.getIsRepair())
                    ? SchedulingAttendanceDailyConstants.IsRepairEnum.IS_REPAIR.getDesc()
                    : SchedulingAttendanceDailyConstants.IsRepairEnum.NOT_REPAIR.getDesc();
            item[7] = SchedulingAttendanceDailyConstants.IsFieldEnum.IS_FIELD.getType().equals(data.getIsField())
                    ? SchedulingAttendanceDailyConstants.IsFieldEnum.IS_FIELD.getDesc()
                    : SchedulingAttendanceDailyConstants.IsFieldEnum.NOT_FIELD.getDesc();
            item[8] = data.getAddress();
            item[9] = data.getRemark();
            item[10] = Objects.nonNull(data.getPics()) ? JacksonUtils.toJsonString(data.getPics()) : "";
            item[11] = data.getCheckTime() != null ? DateUtils.format(data.getCheckTime(), DateUtils.FormatType.COMMON) : "";

            dataList.add(item);
        }
        POIUtils.exportExcel(httpServletResponse, "原始打卡记录", headers, dataList, "原始打卡记录");

    }

    /**
     * 分页查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    @RequirePermissions(values = "originalCheckRecord:list")
    @Operation(summary = "分页获取原始打卡记录列表")
    public Response<Page<MongoOriginalCheckRecordDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());
        int total = mongoOriginalCheckRecordService.queryTotal(request.getParams());

        List<MongoOriginalCheckRecordDto> list = null;
        if (total > 0) {
            list = mongoOriginalCheckRecordService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<MongoOriginalCheckRecordDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<MongoOriginalCheckRecordDto>>ok().wrap(page);
    }

}
